package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.fragment.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.MainV4FragmentAdapter;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.entity.AddFarmAccVO;
import com.digitalchina.lxnsy.entity.ProductPlanListVO;
import com.digitalchina.lxnsy.fragment.FarmAccountListFragment;
import com.digitalchina.lxnsy.fragment.FarmAccountStatisticsFragment;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.view.NoScrollViewPager;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/30 15:50
 * 修改备注：
 * ================================================
 */
public class KeepAccountsMainActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.viewpager)
    NoScrollViewPager viewpager;
    @Bind(R.id.iv_detail)
    ImageView ivDetail;
    @Bind(R.id.tv_detail)
    TextView tvDetail;
    @Bind(R.id.iv_count)
    ImageView ivCount;
    @Bind(R.id.tv_count)
    TextView tvCount;
    @Bind(R.id.iv_add)
    ImageView ivAdd;

    private BottomDialog bottomDialog;
    private AddFarmAccVO addFarmAccVO = new AddFarmAccVO();

    private FarmAccountListFragment fragment1;
    private FarmAccountStatisticsFragment fragment2;
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private MainV4FragmentAdapter mAdapter;
    private final int MODULE_NUMBER = 2;
    private final int INDEX_0 = 0;
    private final int INDEX_1 = 1;
    private int miCurrentIndex;
    private LinearLayout[] maLayouts = new LinearLayout[MODULE_NUMBER];
    private TextView[] mAtv = new TextView[MODULE_NUMBER];
    private ImageView[] mAiv = new ImageView[MODULE_NUMBER];
    // 未选中的图标
    private int[] drawable = {R.drawable.ic_jz_detail_f, R.drawable.ic_jz_countl_f};
    // 选中的图标
    private int[] drawableCheck = {R.drawable.ic_jz_detail_t, R.drawable.ic_jz_countl_t};


    private String productionId = "";// 计划ID

    private String inTime = "";// 进入时间
    private String faroutTime = "";//农事保存退出时间
    private String outTime = "";//   离开时间(通用)

    @Override
    protected void onDestroy() {
        super.onDestroy();
        outTime = DateUtil.getCurrentDate(DateStyle.YYYY_MM_DD_EN_HH_MM_SS_EN);
        BusinessPlan.saveStayOftenInfo(this, productionId, "1", inTime, faroutTime, outTime, mHandler);
    }

    @Override
    public void setLayout() {
        inTime = DateUtil.getCurrentDate(DateStyle.YYYY_MM_DD_EN_HH_MM_SS_EN);
        setContentLayout(R.layout.activity_keep_accounts_main);
        setTitle("农事记账");
    }

    @Override
    public void initMember() {

        mAtv[INDEX_0] = tvDetail;
        mAtv[INDEX_1] = tvCount;

        mAiv[INDEX_0] = ivDetail;
        mAiv[INDEX_1] = ivCount;

        ProductPlanListVO vo = (ProductPlanListVO) getIntent().getSerializableExtra("ProductPlanListVO");

        // 计划ID
        productionId = vo.getId();
        addFarmAccVO.setProductPlanId(productionId);
        // 记账ID 修改时用
        addFarmAccVO.setId("");

        // 底部弹出dialog
        bottomDialog = BottomDialog.create(getSupportFragmentManager());
        bottomDialog.setLayoutRes(R.layout.dialog_keep_accounts);


        fragment1 = new FarmAccountListFragment();
        fragment2 = new FarmAccountStatisticsFragment();

        Bundle bundle = new Bundle();
        bundle.putString("productPlanId", vo.getId());
        fragment1.setArguments(bundle);
        fragment2.setArguments(bundle);

        mFragmentList.add(fragment1);
        mFragmentList.add(fragment2);

        mAdapter = new MainV4FragmentAdapter(getSupportFragmentManager(), mFragmentList);
        viewpager.setAdapter(mAdapter);

        viewpager.setOffscreenPageLimit(MODULE_NUMBER);

        miCurrentIndex = INDEX_0;
        updateColor();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            fragment1.onRefresh();
            fragment2.onRefresh();
        }
    }


    /**
     * 点击底部导航改变当前按钮状态
     */
    private void updateColor() {
        for (int i = 0; i < mAtv.length; i++) {
            if (miCurrentIndex == i) {
                setColor(mAtv[i], getResources().getColor(R.color.app_color));
                setIco(mAiv[i], drawableCheck[i]);
            } else {
                setColor(mAtv[i], getResources().getColor(R.color.colorFontGray));
                setIco(mAiv[i], drawable[i]);
            }
        }
    }

    /**
     * 设置文字颜色
     *
     * @param pTv
     * @param piColor
     */
    private void setColor(TextView pTv, int piColor) {
        pTv.setTextColor(piColor);
    }

    /**
     * 设置图标颜色
     *
     * @param pIv
     * @param index
     */
    private void setIco(ImageView pIv, int index) {
        pIv.setBackgroundResource(index);
    }

    @Override
    public void addListener() {
        bottomDialog.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.findViewById(R.id.tv_out1).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out2).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out3).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out4).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out5).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out6).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out7).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_out8).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_in1).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_in2).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_in3).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.tv_in4).setOnClickListener(KeepAccountsMainActivity.this);
                v.findViewById(R.id.iv_cancel).setOnClickListener(KeepAccountsMainActivity.this);
            }
        });


    }

    @Override
    public void setHandler() {
        mHandler = new Handler();
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_detail, R.id.layout_count, R.id.iv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_detail:
                miCurrentIndex = INDEX_0;
                viewpager.setCurrentItem(miCurrentIndex, false);
                updateColor();
                break;
            case R.id.layout_count:
                miCurrentIndex = INDEX_1;
                viewpager.setCurrentItem(miCurrentIndex, false);
                updateColor();
                break;
            case R.id.iv_add:
                bottomDialog.show();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        int index = 0;
        String type = ""; // 0-收入 1-支出
        switch (view.getId()) {
            case R.id.tv_out1:
                index = 0;
                type = "1";
                break;
            case R.id.tv_out2:
                index = 1;
                type = "1";
                break;
            case R.id.tv_out3:
                index = 2;
                type = "1";
                break;
            case R.id.tv_out4:
                index = 3;
                type = "1";
                break;
            case R.id.tv_out5:
                index = 4;
                type = "1";
                break;
            case R.id.tv_out6:
                index = 5;
                type = "1";
                break;
            case R.id.tv_out7:
                index = 6;
                type = "1";
                break;
            case R.id.tv_out8:
                index = 7;
                type = "1";
                break;
            case R.id.tv_in1:
                index = 8;
                type = "0";
                break;
            case R.id.tv_in2:
                index = 9;
                type = "0";
                break;
            case R.id.tv_in3:
                index = 10;
                type = "0";
                break;
            case R.id.tv_in4:
                index = 11;
                type = "0";
                break;
            case R.id.iv_cancel:
                bottomDialog.dismiss();
                break;

        }

        if (view.getId() != R.id.iv_cancel) {
            addFarmAccVO.setAccountTypeCode(Constant.FARM_ACCOUNT_TYPE_ARRAY[index]);
            addFarmAccVO.setName(Constant.FARM_ACCOUNT_NAME_ARRAY[index]);
            addFarmAccVO.setImage(Constant.FARM_ACCOUNT_IMAGE_ARRAY[index]);
            addFarmAccVO.setType(type);
            bottomDialog.dismiss();

            Intent intent = new Intent(KeepAccountsMainActivity.this, AddFarmAccountActivity.class);
            intent.putExtra("AddFarmAccVO", (Serializable) addFarmAccVO);
            startActivityForResult(intent, 99);
        }

    }
}
